import java.util.Iterator;


public class IterableLong implements Iterable<Boolean> {
  // long value over which we are going to iterate
  private final long value;
  // the only one possible constructor
  public IterableLong(long value) {
    this.value = value;
  }

  @Override
  public Iterator<Boolean> iterator() {
    // anonymous class
    return new Iterator<Boolean>() {
      // anonymous class can have fields (data members)
      // which is the current bit
      long bit = 0;
      @Override
      public boolean hasNext() {
        // long has 64 bits
        // so we have no more than 64 to iterate
        // NOTE: first bit is at position 0
        return bit < 64;
      }
      @Override
      public Boolean next() {
        return (value & ((long)1 << bit++)) != 0;
      }
      @Override
      public void remove() {
        // we dont support remove
        // according JCF we will throw UnsupportedOperationException 
        throw new UnsupportedOperationException();
      }
      
    };
  }
  
  public static void main(String[] args) {
    long aha = 1626294323948573933l;
    System.out.println("expected decimal result is:\n" + aha);
    System.out.println("expected binary result is:\n" + Long.toBinaryString(aha));
    IterableLong hoho = new IterableLong(aha);
    String binary = "";
    for (Boolean isBitSet : hoho) {
      binary = (isBitSet?"1":"0") + binary;
    }
    System.out.println("binary result is:\n" + binary);
    System.out.println("decimal result is:\n" + Long.parseLong(binary, 2));
  }
}
